package txt2vz;

import java.io.IOException;
import java.io.InputStream;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
//import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

import org.apache.tika.Tika;
import org.apache.tika.exception.TikaException;

/**
 * Servlet implementation class Upload
 */
//@WebServlet("/Upload")
//@MultipartConfig
public class UploadServletTomcat7 extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public UploadServletTomcat7() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// String description = request.getParameter("description"); //
		System.out.println("in  upload doppost ");
		Part filePart = request.getPart("postedFile"); ;
		InputStream i = filePart.getInputStream();	
		Tika t = new Tika();
		String s = "om tat sat ";
//		
		try {
		s = t.parseToString(i);					
			//System.out.println(s);
		} catch (TikaException e) {			
			response.getWriter().print("erorr server upload error");
			e.printStackTrace();
		}
		response.getWriter().println(s);
	}

//	private static String getFilename(Part part) {
//		for (String cd : part.getHeader("content-disposition").split(";")) {
//			if (cd.trim().startsWith("filename")) {
//				String filename = cd.substring(cd.indexOf('=') + 1).trim()
//						.replace("\"", "");
//				return filename.substring(filename.lastIndexOf('/') + 1)
//						.substring(filename.lastIndexOf('\\') + 1); // MSIE fix.
//			}
//		}
//		return null;
//	}
}
